﻿using mveEngine;
namespace MetaVideoEditorEvolved.Entity
{
    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows.Forms;

    public class MediaItem : INotifyPropertyChanged
    {
        private bool isCheckedForUpdate;

        private mveEngine.Item mveItem;

        private string name;

        public bool IsCheckedForUpdate
        {
            get
            {
                return this.isCheckedForUpdate;
            }

            set
            {
                this.isCheckedForUpdate = value;

                foreach (var item in this.Childs)
                {
                    item.IsCheckedForUpdate = value;
                }

                this.OnPropertyChanged("IsCheckedForUpdate");
            }
        }

        public mveEngine.Item MveItem
        {
            get
            {
                return this.mveItem;
            }

            set
            {
                this.mveItem = value;
                this.OnPropertyChanged("MveItem");
            }
        }

        public MediaItem()
        {
            this.Childs = new ObservableCollection<MediaItem>();
        }

        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                this.name = value;
                this.OnPropertyChanged("Name");
            }
        }
        
        public Poster PrimaryImage
        {
        	get
        	{
        		return MveItem.PrimaryImage;
        	}
        	
        	set
        	{
        		MveItem.PrimaryImage = value;
        		IsCheckedForUpdate = true;
        		this.OnPropertyChanged("PrimaryImage");
        	}
        }
        
         public Poster PrimaryBackdrop
        {
        	get
        	{
        		return MveItem.PrimaryBackdrop;
        	}
        	
        	set
        	{
        		MveItem.PrimaryBackdrop = value;
        		IsCheckedForUpdate = true;
        		this.OnPropertyChanged("PrimaryBackdrop");
        	}
        }

        public ObservableCollection<MediaItem> Childs
        {
            get;
            set;
        }

        public Guid? MediaId
        {
            get;
            set;
        }

        public TreeNode ToTreeNode()
        {
            var node = new TreeNode();
            node.Name = this.Name;

            if (this.MediaId != null)
            {
                node.Tag = this.MediaId.ToString();
            }

            return node;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public System.Collections.Generic.IEnumerable<MediaItem> AsEnumerable()
        {
            yield return this;

            foreach (var node in this.Childs)
            {
                yield return node;

                foreach (var child in node.Childs)
                {
                    yield return child;

                    foreach (var subChild in child.Childs)
                    {
                        yield return subChild;
                    }
                }
            }
        }
    }
}
